/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import event.JobAlarmEvent;
import event.JobConfirmEvent;
import event.JobRequestEvent;
import event.JobStatusEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import net.sf.appia.core.AppiaEventException;
import net.sf.appia.core.Channel;
import net.sf.appia.core.Direction;
import net.sf.appia.core.Event;
import net.sf.appia.core.Layer;
import net.sf.appia.core.Session;
import net.sf.appia.core.events.channel.ChannelInit;
import util.Erro;
import util.Info;

/**
 *
 * @author Welington
 */
public class ApplicationSession extends Session {
    private ApplicationDataReader reader = null;
    private Class current = ApplicationSession.class;
    
    public ApplicationSession(Layer layer) {
        super(layer);
    }

    @Override
    public void handle(Event event) {
        if (event instanceof ChannelInit) {
            handleChannelInit((ChannelInit) event);
        } else if (event instanceof JobConfirmEvent) {
            handleJobConfirm((JobConfirmEvent) event);
        } else if (event instanceof JobAlarmEvent) {
            handleJobAlarm((JobAlarmEvent) event);
        } else if (event instanceof JobStatusEvent) {
            handleJobStatus((JobStatusEvent) event);
        }
    }

    private void handleChannelInit(ChannelInit init) {
        try {
            init.go();
        } catch (AppiaEventException ex) {
            Erro.showMessage(current, ex);
        }

        if (reader == null) {
            reader = new ApplicationDataReader(init.getChannel());
            reader.start();
        }
    }

    private void handleJobConfirm(JobConfirmEvent conf) {
        Info.showMessage(current, "Received confirmation of request [" + conf.getId() + "]");

        try {
            conf.go();
        } catch (AppiaEventException ex) {
            Erro.showMessage(current, ex);
        }
    }

    private void handleJobAlarm(JobAlarmEvent alarm) {
        Info.showMessage(current, "Received ALARM");

        try {
            alarm.go();
        } catch (AppiaEventException ex) {
            Erro.showMessage(current, ex);
        }
    }

    private void handleJobStatus(JobStatusEvent status) {
        Info.showMessage(current, "Received status (" + status.getStatus().getLabel() +
            ") for request (" + status.getId() + ")");

        try {
            status.go();
        } catch (AppiaEventException ex) {
            Erro.showMessage(current, ex);
        }
    }

    private class ApplicationDataReader extends Thread {
        public boolean ready = false;
        public Channel channel;
        private BufferedReader stdin;
        private int rid = 0;

        public ApplicationDataReader(Channel channel) {
            stdin = new BufferedReader(new InputStreamReader(System.in));
            ready = true;
            if (this.channel == null) {
                this.channel = channel;
            }
            
        }

        @Override
        public void run() {
            boolean running = true;

            while (running) {
                ++rid;
                Info.showMessage(current, "Handling request (" + rid + ")");
                try {
                    String s = stdin.readLine();

                    JobRequestEvent request = new JobRequestEvent();
                    request.setId(rid);
                    request.setString(s);
                    request.asyncGo(channel, Direction.DOWN);
                } catch (AppiaEventException ex) {
                    Erro.showMessage(current, ex);
                } catch (IOException e) {
                    Erro.showMessage(current, e);
                }

                try {
                    Thread.sleep(1500);
                } catch (Exception ex) {
                    Erro.showMessage(current, ex);
                }

                synchronized (this) {
                    if (!ready) {
                        running = false;
                    }
                }
            }
        }
    }
}